package com.mosai.alignpro.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.mosai.alignpro.R;

/**
 * Created by Rays on 2020/3/25.
 */
public class AlertDialog extends Dialog implements View.OnClickListener {

    private TextView tvTitle, tvContent, tvCancel, tvConfirm;
    private View line1;
    private DialogInterface.OnClickListener positiveButtonListener;
    private DialogInterface.OnClickListener negativeButtonListener;

    public AlertDialog(@NonNull Context context) {
        super(context, R.style.LoadingDialog);
        setContentView(R.layout.alert_loading);
        initView(context);
    }

    private void initView(Context context) {
        tvTitle = findViewById(R.id.tvTitle);
        tvContent = findViewById(R.id.tvContent);
        tvCancel = findViewById(R.id.tvCancel);
        tvConfirm = findViewById(R.id.tvConfirm);
        line1 = findViewById(R.id.line1);
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
    }

    @Override
    public void setTitle(CharSequence title) {
        tvTitle.setText(title);
        setVisibility(title, tvTitle);
    }

    public void setMessage(CharSequence message) {
        setMessage(message, Gravity.CENTER);
    }

    public void setMessage(CharSequence message, int gravity) {
        tvContent.setText(message);
        tvContent.setGravity(gravity);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tvCancel) {
            if (negativeButtonListener != null) {
                negativeButtonListener.onClick(this, DialogInterface.BUTTON_NEGATIVE);
            }
        } else {
            if (positiveButtonListener != null) {
                positiveButtonListener.onClick(this, DialogInterface.BUTTON_POSITIVE);
            }
        }
    }

    public void setPositiveButton(CharSequence text, OnClickListener listener) {
        tvConfirm.setText(text);
        setVisibility(text, tvConfirm);
        positiveButtonListener = listener;
        showLine();
    }

    public void setNegativeButton(CharSequence text, OnClickListener listener) {
        tvCancel.setText(text);
        setVisibility(text, tvCancel);
        negativeButtonListener = listener;
        showLine();
    }

    private void setVisibility(CharSequence text, TextView textView) {
        textView.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
    }

    private void showLine() {
        if (tvConfirm.getVisibility() == View.VISIBLE && tvCancel.getVisibility() == View.VISIBLE) {
            line1.setVisibility(View.VISIBLE);
        } else {
            line1.setVisibility(View.GONE);
        }
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        if (window == null) {
            return;
        }
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);
    }

    public static class Builder {
        private CharSequence mTitle;
        private CharSequence mMessage;
        private CharSequence mPositiveButtonText;
        private OnClickListener mPositiveButtonListener;
        private CharSequence mNegativeButtonText;
        private OnClickListener mNegativeButtonListener;
        private boolean mCancelable = true;
        private int mGravity = Gravity.CENTER;
        private Context mContext;

        public Builder(@NonNull Context context) {
            this.mContext = context;
        }

        public AlertDialog.Builder setTitle(@StringRes int titleId) {
            this.mTitle = this.mContext.getText(titleId);
            return this;
        }

        public AlertDialog.Builder setTitle(@Nullable CharSequence title) {
            this.mTitle = title;
            return this;
        }

        public AlertDialog.Builder setMessage(@StringRes int messageId) {
            this.mMessage = this.mContext.getText(messageId);
            return this;
        }

        public AlertDialog.Builder setMessage(@Nullable CharSequence message) {
            this.mMessage = message;
            return this;
        }

        public AlertDialog.Builder setMessageGravity(int gravity) {
            this.mGravity = gravity;
            return this;
        }

        public AlertDialog.Builder setPositiveButton(@StringRes int textId, OnClickListener listener) {
            this.mPositiveButtonText = this.mContext.getText(textId);
            this.mPositiveButtonListener = listener;
            return this;
        }

        public AlertDialog.Builder setPositiveButton(CharSequence text, OnClickListener listener) {
            this.mPositiveButtonText = text;
            this.mPositiveButtonListener = listener;
            return this;
        }

        public AlertDialog.Builder setNegativeButton(@StringRes int textId, OnClickListener listener) {
            this.mNegativeButtonText = this.mContext.getText(textId);
            this.mNegativeButtonListener = listener;
            return this;
        }

        public AlertDialog.Builder setNegativeButton(CharSequence text, OnClickListener listener) {
            this.mNegativeButtonText = text;
            this.mNegativeButtonListener = listener;
            return this;
        }

        public AlertDialog.Builder setCancelable(boolean cancelable) {
            this.mCancelable = cancelable;
            return this;
        }

        public AlertDialog create() {
            AlertDialog dialog = new AlertDialog(this.mContext);
            dialog.setTitle(mTitle);
            dialog.setMessage(mMessage, mGravity);
            dialog.setPositiveButton(mPositiveButtonText, mPositiveButtonListener);
            dialog.setNegativeButton(mNegativeButtonText, mNegativeButtonListener);
            dialog.setCancelable(mCancelable);
            return dialog;
        }

        public AlertDialog show() {
            AlertDialog dialog = this.create();
            dialog.show();
            return dialog;
        }
    }
}
